import logging

import pytest

from resource.pom.gallery import Gallery
from utils.manager import MatchRules, Images


@pytest.fixture(scope='function', autouse=True)
def func_setup_teardown(device):
    logging.info('------用例前置：预置图片资源------')
    app = Gallery
    device.mediatool_delete_all()
    device.hdc_file_send(app.photo1, app.photo_dir)
    device.mediatool_send(app.photo_dir)
    yield
    logging.info('------用例后置：删除预置资源------')
    device.mediatool_delete_all()


class Test:
    app = Gallery

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.选择一张图片点击进入')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.refresh_layout()
        device.click_element(device.get_element_by_key('TabPhotos'))
        device.refresh_layout()
        device.click_element(device.get_element_by_key('Gesture_1'))
        logging.info('预期结果1.进入到图片')
        device.refresh_layout()
        device.assert_key_exist('PhotoBrowserBg')
        device.assert_key_exist('ActionBarButtonBack')
        device.assert_key_exist('PhotoBrowserDateTitle')
        device.assert_key_exist('DetailMenuPanel')
        device.assert_key_exist('ActionBarButtonInfo')
        device.assert_key_exist('ToolBarButtonFavor')
        device.assert_key_exist('ToolBarButtonEdit')
        device.assert_key_exist('ToolBarButtonDelete')
        device.assert_key_exist('ToolBarButtonMore')

        logging.info('步骤2.点击编辑按钮')
        edit_btn = device.get_element_by_key('ToolBarButtonEdit')
        device.click_element(edit_btn)
        logging.info('预期结果2.进入到图片编辑界面')
        device.refresh_layout()
        device.assert_key_exist('PhotoEditBackButton')
        device.assert_text_exist('编辑')
        device.assert_key_exist('PhotoEditSaveButton')
        device.assert_key_exist('ActionButtonEditCropFreedom')
        device.assert_key_exist('ActionButtonEditCrop_4_3')
        device.assert_key_exist('ActionButtonEditCrop_3_4')
        device.assert_key_exist('ActionButtonEditCrop_1_1')
        device.assert_key_exist('ActionButtonEditCrop_3_2')
        device.assert_key_exist('ActionButtonEditCrop_2_3')
        device.assert_key_exist('ActionButtonEditCrop_9_16')
        device.assert_key_exist('ActionButtonRotate')
        device.assert_text_exist('旋转')
        device.assert_type_exist('Canvas')
        device.assert_key_exist('ActionButtonMirror')
        device.assert_text_exist('镜像')
        device.assert_key_exist('ActionButtonEditToolBarCrop')
        device.assert_text_exist('修剪')
        # 编辑前截图
        before_edit_pic = device.save_snapshot_to_local('before_edit.jpeg')

        logging.info('步骤3.点击旋转/镜像/修剪按钮')
        rotate = device.get_element_by_key('ActionButtonRotate')
        mirror = device.get_element_by_key('ActionButtonMirror')
        crop_3_4 = device.get_element_by_key('ActionButtonEditCrop_3_4')
        device.click_element(rotate)
        # 底部旋转刻度线
        rotate_range = device.get_element_by_type('Canvas', index=1)
        x1, y1, x2, y2 = device.get_element_area(rotate_range)
        device.swipe((x1 + x2) // 2, (y1 + y2) // 2, x1, (y1 + y2) // 2)
        device.click_element(mirror)
        device.click_element(crop_3_4)
        # device.swipe(626, 888, 448, 650)
        logging.info('预期结果3.1图片可按逆时针旋转/可显示镜像图片/可按修剪框进行修剪保存，保存后的图片与进行的操作显示一致')
        device.wait(1)
        current_edit_snapshot = device.save_snapshot_to_local('edit.jpeg')
        similarity_edit = Images.compare_image_similarity(current_edit_snapshot, self.app.standard_edited_pic)
        assert similarity_edit > 0.7, '编辑后的图片和标准图相似度过低'
        logging.info('预期结果3.2重置后还原到编辑前的样式')
        device.refresh_layout()
        device.click_element(device.get_element_by_text('重置'))
        # 重置后截图
        after_reset_pic = device.save_snapshot_to_local('after_reset.jpeg')
        similarity_reset = Images.compare_image_similarity(before_edit_pic, after_reset_pic)
        assert similarity_reset > 0.7, '重置后的图和原图差异过大，重置失效'

        # 重新编辑
        device.click_element(rotate)
        # 底部旋转刻度线
        rotate_range = device.get_element_by_type('Canvas', index=1)
        x1, y1, x2, y2 = device.get_element_area(rotate_range)
        device.swipe((x1 + x2) // 2, (y1 + y2) // 2, x1, (y1 + y2) // 2)
        device.click_element(mirror)
        device.click_element(crop_3_4)
        logging.info('步骤4.1 点击右上角保存按钮，点击取消')
        save_btn = device.get_element_by_key('PhotoEditSaveButton')
        device.click_element(save_btn)
        device.refresh_layout()
        save_as_new_btn = device.get_element_by_text('另存')
        cover_btn = device.get_element_by_text('覆盖原图')
        cancel_btn = device.get_element_by_text('取消')
        device.click_element(cancel_btn)
        logging.info('预期结果4.1选择取消返回至编辑界面')
        device.refresh_layout()
        device.assert_key_exist('PhotoEditBackButton')
        device.assert_text_exist('编辑')
        device.assert_key_exist('PhotoEditSaveButton')
        device.assert_key_exist('ActionButtonEditCropFreedom')
        device.assert_key_exist('ActionButtonEditCrop_4_3')
        device.assert_key_exist('ActionButtonEditCrop_3_4')
        device.assert_key_exist('ActionButtonEditCrop_1_1')
        device.assert_key_exist('ActionButtonEditCrop_3_2')
        device.assert_key_exist('ActionButtonEditCrop_2_3')
        device.assert_key_exist('ActionButtonEditCrop_9_16')
        device.assert_key_exist('ActionButtonRotate')
        device.assert_text_exist('旋转')
        device.assert_type_exist('Canvas')
        device.assert_key_exist('ActionButtonMirror')
        device.assert_text_exist('镜像')
        device.assert_key_exist('ActionButtonEditToolBarCrop')
        device.assert_text_exist('修剪')

        logging.info('步骤4.2点击右上角保存按钮，点击另存')
        device.click_element(save_btn)
        device.click_element(save_as_new_btn)
        logging.info('预期结果4.2选择另存则编辑后的图片会生成一张新的图片')
        device.refresh_layout()
        # current_pic_attrs = device.get_element_by_key('browserFocus_file', rule=MatchRules.Contain)
        current_pic_crop_area = (0, 82, 720, 1122)
        save_as = device.save_snapshot_to_local('save_as.jpeg')
        Images.crop_picture(save_as, *current_pic_crop_area)
        similarity_save_as = Images.compare_image_similarity(save_as, self.app.standard_save_as_pic)
        device.swipe(600, 640, 100, 640)
        device.refresh_layout()
        org_pic_crop_area = (0, 82, 720, 1122)
        org_pic = device.save_snapshot_to_local('org.jpeg')
        Images.crop_picture(org_pic, *org_pic_crop_area)
        similarity_org = Images.compare_image_similarity(org_pic, self.app.standard_org_pic)
        assert similarity_save_as > 0.6 and similarity_org > 0.6, '另存失败'

        logging.info('步骤4.3点击右上角保存按钮，点击覆盖原图')
        device.click_element(edit_btn)
        device.wait(1)
        device.click_element(rotate)
        device.wait(0.2)
        device.click_element(mirror)
        device.wait(0.2)
        device.click_element(save_btn)
        device.wait(0.2)
        device.click_element(cover_btn)
        logging.info('预期结果4.选择覆盖原图则编辑后的图片会把原图片覆盖掉')
        device.refresh_layout()
        cover_pic_attrs = device.get_element_by_key('browserFocus_file', rule=MatchRules.Contain)
        cover_crop_area = device.get_element_area(cover_pic_attrs)
        cover_pic = device.save_snapshot_to_local('cover.jpeg')
        Images.crop_picture(cover_pic, *cover_crop_area)
        similarity2 = Images.compare_image_similarity(cover_pic, self.app.standard_covered_pic)
        assert similarity2 > 0.6, '原图片覆盖失败'
